//Source file: D:\\WTK104\\apps\\Toddtris\\src\\com\\toddsoft\\toddtris\\midlet\\ToddtrisMIDlet.java

package com.toddsoft.toddtris.model;

import com.toddsoft.toddtris.model.Well;
import com.toddsoft.toddtris.model.WellListener;
import java.net.*;

public class HallOfFame implements WellListener {

    private HallOfFameDataProvider dataProvider;


    public HallOfFame(HallOfFameDataProvider dp,
                      Well well) {
        this.dataProvider = dp;
        well.addListener(this);
    }

    private void handleScore(Well well) {
        /* send a request to see if this score qualifies */
        boolean qualified = false;

        String gameType = (well.getTargetLines() == 0) ? "M" : "TT";

        /* get minimum score to get on the hall of fame */
        String urlScoreData = "?game_type=" + gameType
                              + "&line_count=" + well.getLines()
                              + "&time=" + well.getRunningTimeInMillis();

        String urlStr = dataProvider.getCodeBase() 
                        + "check_high_score.php"
                        + urlScoreData;
        try {
            URL url = new URL(urlStr);
            HttpURLConnection con = (HttpURLConnection)url.openConnection();
            qualified = con.getHeaderField("qualified").equals("y");
        } catch (Exception e) {
            e.printStackTrace();
        }

        /* if made high enough score, then submit */
        if (qualified) {

            String[] highScoreDetails 
                    = dataProvider.getPlayerHighScoreDetails();

            if ((highScoreDetails == null)
                || (highScoreDetails.length != 2)) {

                /* don't carry on any further */
                return;
            }

            String username = highScoreDetails[0].trim();
            String userfrom = highScoreDetails[1].trim();

            if (username.length() == 0) {
                /* don't carry on any further */
                return;
            }

            urlStr = dataProvider.getCodeBase()
                    + "receive_high_score.php"
                    + urlScoreData
                    + "&username=" 
                        + username
                    + "&userfrom=" 
                        + userfrom;
            try {
                URL url = new URL(urlStr);
                HttpURLConnection con = (HttpURLConnection)url.openConnection();
                String rank = con.getHeaderField("netris_rank");
                System.out.println("rank: " + rank);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * WellListener Methods
     */

    /*
     * @param well
     */
    public void gameOver(Well well) {

        /* can only get a high score if this was a marathon game */
        if (well.getTargetLines() == 0) {
            this.handleScore(well);
        }
    }

    /**
     * @param well
     */
    public void complete(Well well) {

        /* must be a time trial, see if the time is good enough */
        this.handleScore(well);
    }
    
    /**
     * @param well
     */
    public void wellChanged(Well well) {
        // do nothing     
    }
    
    /**
     * @param well
     * @param linesMade
     */
    public void madeLines(Well well, int linesMade) {
        // do nothing     
    }
    
    /**
     * @param well
     */
    public void wellReset(Well well) {
        // do nothing     
    }
    
    /**
     * @param well
     */
    public void blockLanded(Well well) {
        // do nothing     
    }
}
